package com.zzt.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zzt.dao.MemberinfoMapper;
import com.zzt.entity.Memberinfo;
import com.zzt.entity.Myorder;
import com.zzt.entity.PayNeed;
import com.zzt.saleDesign.FullMinusDecorator;
import com.zzt.saleDesign.OriginalPrice;
import com.zzt.saleDesign.Sale;
import com.zzt.saleDesign.VipMinusDecorator;
import com.zzt.service.MyorderService;
import com.zzt.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
public class MyOrderController {

    @Resource
    MyorderService myorderService;
    @Resource
    MemberinfoMapper memberinfoMapper;
    @Resource
    RedisUtils redisUtils;

    @Autowired  //用我们自己的
    @Qualifier("redisTemplate")
    RedisTemplate redisTemplate;

    @GetMapping("/user/toPay/{totalPrice}")
    public PayNeed toPay(@PathVariable("totalPrice") int totalPrice, HttpServletRequest request){

        HttpSession session = request.getSession();
        Memberinfo user = (Memberinfo)session.getAttribute("user");
        //优惠打折sale
        //原价
        Sale originalPrice = new OriginalPrice();
        //后打折
        Sale vipMinusDecorator = new VipMinusDecorator(originalPrice);
        //先满减
        //从下往上的顺序
        Sale fullMinusDecorator = new FullMinusDecorator(vipMinusDecorator);
        double discount = fullMinusDecorator.discount(totalPrice);
        double salePrice = (double) Math.round(discount * 100) / 100 ;//小数点后2位
        System.out.println("最终实付价格："+salePrice);

        PayNeed payNeed = new PayNeed(user, totalPrice,salePrice);
        session.setAttribute("payNeed",payNeed);
        return payNeed;
    }

    @GetMapping("/user/surePay")
    public Myorder createOrder(@RequestParam("nickname") String nickname,@RequestParam("pwd") String pwd,
                               @RequestParam("mno") Integer mno,@RequestParam("totalPrice") String totalPrice
                                ,@RequestParam("discount") String discount) throws JsonProcessingException {

        Memberinfo memberinfo = memberinfoMapper.selectByPrimaryKey(mno);
        if(!new BCryptPasswordEncoder().matches(pwd,memberinfo.getPwd())){   //密码错误
            return new Myorder(-1F,"error",new Date(),-1);
        }
        Myorder myorder = new Myorder(Float.valueOf(discount),nickname,new Date(),mno);
        Myorder order = myorderService.createOrder(myorder);
        return order;
    }

    @GetMapping("/user/showAllOrders")
    public List<Myorder> showAllOrders(HttpServletRequest request){
        HttpSession session = request.getSession();
        Memberinfo user = (Memberinfo)session.getAttribute("user");
        List<Myorder> myorders = myorderService.showAllOrders(user.getMno());
        return myorders;
    }

}
